<template>
  <DimensionItemContentInput v-bind="$attrs">
    <template #append>
      <GdButton
        type="primary"
        ref="BizUiGenerateCodeBtnRef"
        @click="Wrap_onBizUiClickGenerateCodeBtn"
        >生成编码
      </GdButton>
    </template>
  </DimensionItemContentInput>
</template>
<script>
export default {
  inheritAttrs: false,
};
</script>
<script setup>
import {
  computed,
  watch,
  inject,
  useAttrs,
  ref,
  onMounted,
  getCurrentInstance,
} from "vue";
import GdButton from "@gd-accbuild-ui/gd-ui/gd-button/gd-button.vue";
import { globalConfig } from "@gd-accbuild-core/config";
import {
  isSuccessReqBackend,
  getBackendData,
} from "@gd-accbuild-core/config/utils";
import DimensionItemContentInput from "../../gd-components/input/input.vue";
import { getAllBindEvents } from "../../useComponentEvents";
import RandExp from "randexp";
const { proxy: vm } = getCurrentInstance();
const _attrs = useAttrs();
const allBindEvents = getAllBindEvents(_attrs, false, ["events"], { vm,globalConfig });

const BizUiGenerateCodeBtnRef = ref(null);
const bizUiAutoComplete = computed(() => {
  return _attrs["meta-item"]?.componentAttr?.bizUiAutoComplete ?? true;
});
onMounted(() => {
  if (bizUiAutoComplete.value) {
    setTimeout(function () {
      BizUiGenerateCodeBtnRef.value.$el.click();
    }, 2000);
  }
});
const onBizUiClickGenerateCodeBtn = async (data) => {
  let code = "";
  if (data.isFontendGen && data.generateAlgorithm === "regex") {
    code = new RandExp(new RegExp(data.expression)).gen();
  } else {
    const res = await globalConfig.generateCode({
      moduleCode: "gd_sys",
      vmCode: "Common",
      strategyName: "generateCode",
      requestType: "post",
      data,
    });
    if (isSuccessReqBackend(res)) {
      const data = getBackendData(res);
      code = data.code;
    }
  }
  _attrs["form-data"][_attrs["meta-item"].key] = code;
};
const Wrap_onBizUiClickGenerateCodeBtn = async () => {
  if (allBindEvents.hasOwnProperty("onBizUiClickGenerateCodeBtn")) {
    allBindEvents.onBizUiClickGenerateCodeBtn({
      orgMethod: onBizUiClickGenerateCodeBtn,
    });
  }
};
</script>
<style lang="scss" scoped></style>
